home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / awt / compon~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  27.4 KB  |  1,120 lines

  1. /*
  2.  * @(#)Component.java    1.65 95/12/14 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.io.PrintStream;
  22. import java.awt.peer.ComponentPeer;
  23. import java.awt.image.ImageObserver;
  24. import java.awt.image.ImageProducer;
  25. import java.awt.image.ColorModel;
  26.  
  27. /**
  28.  * A generic Abstract Window Toolkit component. 
  29.  *
  30.  * @version     1.65, 12/14/95
  31.  * @author     Arthur van Hoff
  32.  * @author     Sami Shaio
  33.  */
  34. public abstract class Component implements ImageObserver {
  35.     /**
  36.      * The peer of the component. The peer implements the component's
  37.      * behaviour. The peer is set when the Component is added to a 
  38.      * container that also is a peer.
  39.      * @see #getPeer
  40.      * @see #addNotify
  41.      * @see #removeNotify
  42.      */
  43.     ComponentPeer peer;
  44.  
  45.     /**
  46.      * The parent of the object. It may be null for toplevel components.
  47.      * @see #getParent
  48.      */
  49.     Container parent;
  50.  
  51.     /**
  52.      * The x position of the component in the parent's coordinate system.
  53.      * @see #position
  54.      */
  55.     int x;
  56.  
  57.     /**
  58.      * The y position of the component in the parent's coordinate system.
  59.      * @see #position
  60.      */
  61.     int y;
  62.  
  63.     /**
  64.      * The width of the component.
  65.      * @see #size
  66.      */
  67.     int width;
  68.  
  69.     /**
  70.      * The height of the component.
  71.      * @see #size
  72.      */
  73.     int height;
  74.  
  75.     /**
  76.      * The foreground color for this component.
  77.      * @see #getForeground
  78.      * @see #setForeground
  79.      */
  80.     Color    foreground;
  81.  
  82.     /**
  83.      * The background color for this component.
  84.      * @see #getBackground
  85.      * @see #setBackground
  86.      */
  87.     Color    background;
  88.  
  89.     /**
  90.      * The font used by this component.
  91.      * @see #getFont
  92.      * @see #setFont
  93.      */
  94.     Font    font;
  95.  
  96.     /**
  97.      * True when the object is visible. An object that is not
  98.      * visible is not drawn on the screen.
  99.      * @see #isVisible
  100.      * @see #show
  101.      * @see #hide
  102.      */
  103.     boolean visible = true;
  104.  
  105.     /**
  106.      * True when the object is enabled. An object that is not
  107.      * enabled does not interact with the user.
  108.      * @see #isEnabled
  109.      * @see #enable
  110.      * @see #disable
  111.      */
  112.     boolean enabled = true;
  113.  
  114.     /** 
  115.      * True when the object is valid. An invalid object needs to
  116.      * be layed out. This flag is set to false when the object
  117.      * size is changed.
  118.      * @see #isValid
  119.      * @see #validate
  120.      * @see #invalidate
  121.      */
  122.     boolean valid = false;
  123.  
  124.     /**
  125.      * Constructs a new Component. Components should not be subclassed or 
  126.      * instantiated directly.
  127.      */
  128.     Component() {
  129.     }
  130.  
  131.     /**
  132.      * Gets the parent of the component.
  133.      */
  134.     public Container getParent() {
  135.     return parent;
  136.     }
  137.  
  138.     /**
  139.      * Gets the peer of the component.
  140.      */
  141.     public ComponentPeer getPeer() {
  142.     return peer;
  143.     }
  144.  
  145.     /**
  146.      * Gets the toolkit of the component. This toolkit is
  147.      * used to create the peer for this component.  Note that
  148.      * the Frame which contains a Component controls which
  149.      * toolkit is used so if the Component has not yet been
  150.      * added to a Frame or if it is later moved to a different
  151.      * Frame, the toolkit it uses may change.
  152.      */
  153.     public Toolkit getToolkit() {
  154.     ComponentPeer peer = this.peer;
  155.     if (peer != null) {
  156.         return peer.getToolkit();
  157.     }
  158.     Container parent = this.parent;
  159.     if (parent != null) {
  160.         return parent.getToolkit();
  161.     }
  162.     return Toolkit.getDefaultToolkit();
  163.     }
  164.  
  165.     /**
  166.      * Checks if this Component is valid. Components are invalidated when
  167.      * they are first shown on the screen.
  168.      * @see #validate
  169.      * @see #invalidate
  170.      */
  171.     public boolean isValid() {
  172.     return (peer != null) && valid;
  173.     }
  174.  
  175.     /**
  176.      * Checks if this Component is visible. Components are initially visible 
  177.      * (with the exception of top level components such as Frame).
  178.      * @see #show
  179.      * @see #hide
  180.      */
  181.     public boolean isVisible() {
  182.     return visible;
  183.     }
  184.  
  185.     /**
  186.      * Checks if this Component is showing on screen. This means that the 
  187.      * component must be visible, and it must be in a container that is 
  188.      * visible and showing.
  189.      * @see #show
  190.      * @see #hide
  191.      */
  192.     public boolean isShowing() {
  193.     if (visible && (peer != null)) {
  194.         Container parent = this.parent;
  195.         return (parent == null) || parent.isShowing();
  196.     }
  197.     return false;
  198.     }
  199.  
  200.     /**
  201.      * Checks if this Component is enabled. Components are initially enabled.
  202.      * @see #enable
  203.      * @see #disable
  204.      */
  205.     public boolean isEnabled() {
  206.     return enabled;
  207.     }
  208.  
  209.     /** 
  210.      * Returns the current location of this component.
  211.      * The location will be in the parent's coordinate space.
  212.      * @see #move
  213.      */
  214.     public Point location() {
  215.     return new Point(x, y);
  216.     }
  217.  
  218.     /** 
  219.      * Returns the current size of this component.
  220.      * @see #resize
  221.      */
  222.     public Dimension size() {
  223.     return new Dimension(width, height);
  224.     }
  225.  
  226.     /** 
  227.      * Returns the current bounds of this component.
  228.      * @see #reshape
  229.      */
  230.     public Rectangle bounds() {
  231.     return new Rectangle(x, y, width, height);
  232.     }
  233.  
  234.     /**
  235.      * Enables a component.
  236.      * @see #isEnabled
  237.      * @see #disable
  238.      */
  239.     public synchronized void enable() {
  240.     if (!enabled) {
  241.         enabled = true;
  242.         if (peer != null) {
  243.         peer.enable();
  244.         }
  245.     }
  246.     }
  247.  
  248.     /**
  249.      * Conditionally enables a component.
  250.      * @param cond if true, enables component; disables otherwise.
  251.      * @see #enable
  252.      * @see #disable
  253.      */
  254.     public void enable(boolean cond) {
  255.     if (cond) {
  256.         enable();
  257.     } else {
  258.         disable();
  259.     }
  260.     }
  261.  
  262.     /**
  263.      * Disables a component.
  264.      * @see #isEnabled
  265.      * @see #enable
  266.      */
  267.     public synchronized void disable() {
  268.     if (enabled) {
  269.         enabled = false;
  270.         if (peer != null) {
  271.         peer.disable();
  272.         }
  273.     }
  274.     }
  275.  
  276.     /**
  277.      * Shows the component. 
  278.      * @see #isVisible
  279.      * @see #hide
  280.      */
  281.     public synchronized void show() {
  282.     if (!visible) {
  283.         visible = true;
  284.         if (peer != null) {
  285.         peer.show();
  286.         if (parent != null) {
  287.             parent.invalidate();
  288.         }
  289.         }
  290.     }
  291.     }
  292.  
  293.     /**
  294.      * Conditionally shows the component. 
  295.      * @param cond if true, it shows the component; hides otherwise.
  296.      * @see #show
  297.      * @see #hide
  298.      */
  299.     public void show(boolean cond) {
  300.     if (cond) {
  301.         show();
  302.     } else {
  303.         hide();
  304.     }
  305.     }
  306.  
  307.     /**
  308.      * Hides the component.
  309.      * @see #isVisible
  310.      * @see #hide
  311.      */
  312.     public synchronized void hide() {
  313.     if (visible) {
  314.         visible = false;
  315.         if (peer != null) {
  316.         peer.hide();
  317.         if (parent != null) {
  318.             parent.invalidate();
  319.         }
  320.         }
  321.     }
  322.     }
  323.  
  324.     /**
  325.      * Gets the foreground color. If the component does
  326.      * not have a foreground color, the foreground color
  327.      * of its parent is returned.
  328.      * @see #setForeground
  329.      */
  330.     public Color getForeground() {
  331.     Color foreground = this.foreground;
  332.     if (foreground != null) {
  333.         return foreground;
  334.     }
  335.     Container parent = this.parent;
  336.     return (parent != null) ? parent.getForeground() : null;
  337.     }
  338.  
  339.     /** 
  340.      * Sets the foreground color.
  341.      * @param c the Color
  342.      * @see #getForeground
  343.      */
  344.     public synchronized void setForeground(Color c) {
  345.     foreground = c;
  346.     if (peer != null) {
  347.         c = getForeground();
  348.         if (c != null) {
  349.         peer.setForeground(c);
  350.         }
  351.     }
  352.     }
  353.  
  354.     /**
  355.      * Gets the background color. If the component does
  356.      * not have a background color, the background color
  357.      * of its parent is returned.
  358.      * @see #setBackground
  359.      */
  360.     public Color getBackground() {
  361.     Color background = this.background;
  362.     if (background != null) {
  363.         return background;
  364.     }
  365.     Container parent = this.parent;
  366.     return (parent != null) ? parent.getBackground() : null;
  367.     }
  368.  
  369.     /** 
  370.      * Sets the background color.
  371.      * @param c the Color
  372.      * @see #getBackground
  373.      */
  374.     public synchronized void setBackground(Color c) {
  375.     background = c;
  376.     if (peer != null) {
  377.         c = getBackground();
  378.         if (c != null) {
  379.         peer.setBackground(c);
  380.         }
  381.     }
  382.     }
  383.  
  384.     /**
  385.      * Gets the font of the component. If the component does
  386.      * not have a font, the font of its parent is returned.
  387.      * @see #setFont
  388.      */
  389.     public Font getFont() {
  390.     Font font = this.font;
  391.     if (font != null) {
  392.         return font;
  393.     }
  394.     Container parent = this.parent;
  395.     return (parent != null) ? parent.getFont() : null;
  396.     }
  397.  
  398.     /** 
  399.      * Sets the font of the component.
  400.      * @param f the font
  401.      * @see #getFont
  402.      */
  403.     public synchronized void setFont(Font f) {
  404.     font = f;
  405.     if (peer != null) {
  406.         f = getFont();
  407.         if (f != null) {
  408.         peer.setFont(f);
  409.         }
  410.     }
  411.     }
  412.  
  413.     /**
  414.      * Gets the ColorModel used to display the component on the output device.
  415.      * @see ColorModel
  416.      */
  417.     public synchronized ColorModel getColorModel() {
  418.     if (peer == null) {
  419.         return getToolkit().getColorModel();
  420.     } else {
  421.         return peer.getColorModel();
  422.     }
  423.     }
  424.  
  425.     /** 
  426.      * Moves the Component to a new location. The x and y coordinates
  427.      * are in the parent's coordinate space.
  428.      * @param x the x coordinate
  429.      * @param y the y coordinate
  430.      * @see #location
  431.      * @see #reshape
  432.      */
  433.     public void move(int x, int y) {
  434.     reshape(x, y, width, height);
  435.     }
  436.  
  437.     /**
  438.      * Resizes the Component to the specified width and height.
  439.      * @param width the width of the component
  440.      * @param height the height of the component
  441.      * @see #size
  442.      * @see #reshape
  443.      */
  444.     public void resize(int width, int height) {
  445.     reshape(x, y, width, height);
  446.     }
  447.  
  448.     /** 
  449.      * Resizes the Component to the specified dimension.
  450.      * @param d the component dimension
  451.      * @see #size
  452.      * @see #reshape
  453.      */
  454.     public void resize(Dimension d) {
  455.     reshape(x, y, d.width, d.height);
  456.     }
  457.  
  458.     /** 
  459.      * Reshapes the Component to the specified bounding box.
  460.      * @param x the x coordinate
  461.      * @param y the y coordinate
  462.      * @param width the width of the component
  463.      * @param height the height of the component
  464.      * @see #bounds
  465.      * @see #move
  466.      * @see #resize
  467.      */
  468.     public synchronized void reshape(int x, int y, int width, int height) {
  469.     boolean resized = (this.width != width) || (this.height != height);
  470.  
  471.     if (resized || (this.x != x) || (this.y != y)) {
  472.         this.x = x;
  473.         this.y = y;
  474.         this.width = width;
  475.         this.height = height;
  476.         if (peer != null) {
  477.         peer.reshape(x, y, width, height);
  478.         if (resized) {
  479.             invalidate();
  480.         }
  481.         if (parent != null) {
  482.             parent.invalidate();
  483.         }
  484.         }
  485.     }
  486.     }
  487.  
  488.     /** 
  489.      * Returns the preferred size of this component.
  490.      * @see #minimumSize
  491.      * @see LayoutManager
  492.      */
  493.     public Dimension preferredSize() {
  494.     ComponentPeer peer = this.peer;
  495.     return (peer != null) ? peer.preferredSize() : minimumSize();
  496.     }
  497.  
  498.     /**
  499.      * Returns the minimum size of this component.
  500.      * @see #preferredSize
  501.      * @see LayoutManager
  502.      */
  503.     public Dimension minimumSize() {
  504.     ComponentPeer peer = this.peer;
  505.     return (peer != null) ? peer.minimumSize() : size();
  506.     }
  507.  
  508.     /**
  509.      * Lays out the component. This is usually called when the
  510.      * component is validated.
  511.      * @see #validate
  512.      * @see LayoutManager
  513.      */
  514.     public void layout() {
  515.     }
  516.  
  517.     /** 
  518.      * Validates a component.  
  519.      * @see #invalidate
  520.      * @see #layout
  521.      * @see LayoutManager
  522.      */
  523.     public void validate() {
  524.     while ((!valid) && (peer != null)) {
  525.         valid = true;
  526.         layout();
  527.     }
  528.     }
  529.  
  530.     /** 
  531.      * Invalidates a component.
  532.      * @see #validate
  533.      * @see #layout
  534.      * @see LayoutManager
  535.      */
  536.     public void invalidate() {
  537.     valid = false;
  538.     }
  539.  
  540.     /**
  541.      * Gets a Graphics context for this component. This method will
  542.      * return null if the component is currently not on the screen.
  543.      * @see #paint
  544.      */
  545.     public Graphics getGraphics() {
  546.     ComponentPeer peer = this.peer;
  547.     return (peer != null) ? peer.getGraphics() : null;
  548.     }
  549.  
  550.     /**
  551.      * Gets the font metrics for this component. This will
  552.      * return null if the component is currently not on the screen.
  553.      * @param font the font
  554.      * @see #getFont
  555.      */
  556.     public FontMetrics getFontMetrics(Font font) {
  557.     ComponentPeer peer = this.peer;
  558.     return (peer != null)
  559.         ? peer.getFontMetrics(font)
  560.         : getToolkit().getFontMetrics(font);
  561.     }
  562.  
  563.     /** 
  564.      * Paints the component.
  565.      * @param g the specified Graphics window
  566.      * @see #update
  567.      */
  568.     public void paint(Graphics g) {
  569.     }
  570.  
  571.     /** 
  572.      * Updates the component. This method is called in
  573.      * response to a call to repaint. You can assume that
  574.      * the background is not cleared.
  575.      * @param g the specified Graphics window
  576.      * @see #paint
  577.      * @see #repaint
  578.      */
  579.     public void update(Graphics g) {
  580.     g.setColor(getBackground());
  581.     g.fillRect(0, 0, width, height);
  582.     g.setColor(getForeground());
  583.     paint(g);
  584.     }
  585.  
  586.     /**
  587.      * Paints the component and its subcomponents.
  588.      * @param g the specified Graphics window
  589.      * @see #paint
  590.      */
  591.     public void paintAll(Graphics g) {
  592.     ComponentPeer peer = this.peer;
  593.     if (visible && (peer != null)) {
  594.         validate();
  595.         peer.paint(g);
  596.     }
  597.     }
  598.  
  599.     /** 
  600.      * Repaints the component. This will result in a
  601.      * call to update as soon as possible.
  602.      * @see #paint
  603.      */
  604.     public void repaint() {
  605.     repaint(0, 0, 0, width, height);
  606.     }
  607.  
  608.     /** 
  609.      * Repaints the component. This will result in a
  610.      * call to update within <em>tm</em> milliseconds.
  611.      * @param tm maximum time in milliseconds before update
  612.      * @see #paint
  613.      */
  614.     public void repaint(long tm) {
  615.     repaint(tm, 0, 0, width, height);
  616.     }
  617.  
  618.     /** 
  619.      * Repaints part of the component. This will result in a
  620.      * call to update as soon as possible.
  621.      * @param x the x coordinate
  622.      * @param y the y coordinate
  623.      * @param width the width 
  624.      * @param height the height 
  625.      * @see #repaint
  626.      */
  627.     public void repaint(int x, int y, int width, int height) {
  628.     repaint(0, x, y, width, height);
  629.     }
  630.  
  631.     /** 
  632.      * Repaints part of the component. This will result in a
  633.      * call to update width <em>tm</em> millseconds.
  634.      * @param tm maximum time in milliseconds before update
  635.      * @param x the x coordinate
  636.      * @param y the y coordinate
  637.      * @param width the width 
  638.      * @param height the height 
  639.      * @see #repaint
  640.      */
  641.     public void repaint(long tm, int x, int y, int width, int height) {
  642.     ComponentPeer peer = this.peer;
  643.     if ((peer != null) && (width > 0) && (height > 0)) {
  644.         peer.repaint(tm, x, y, width, height);
  645.     }
  646.     }
  647.  
  648.     /**
  649.      * Prints this component. The default implementation of this
  650.      * method calls paint.
  651.      * @param g the specified Graphics window
  652.      * @see #paint
  653.      */
  654.     public void print(Graphics g) {
  655.     paint(g);
  656.     }
  657.  
  658.     /**
  659.      * Prints the component and its subcomponents.
  660.      * @param g the specified Graphics window
  661.      * @see #print
  662.      */
  663.     public void printAll(Graphics g) {
  664.     ComponentPeer peer = this.peer;
  665.     if (visible && (peer != null)) {
  666.         validate();
  667.         peer.print(g);
  668.     }
  669.     }
  670.  
  671.     /**
  672.      * Repaints the component when the image has changed.
  673.      * @return true if image has changed; false otherwise.
  674.      */
  675.     public boolean imageUpdate(Image img, int flags,
  676.                    int x, int y, int w, int h) {
  677.     int rate = -1;
  678.     if ((flags & (FRAMEBITS|ALLBITS)) != 0) {
  679.         rate = 0;
  680.     } else if ((flags & SOMEBITS) != 0) {
  681.         String isInc = System.getProperty("awt.image.incrementaldraw");
  682.         if (isInc == null || isInc.equals("true")) {
  683.         String incRate = System.getProperty("awt.image.redrawrate");
  684.         try {
  685.             rate = (incRate != null) ? Integer.parseInt(incRate) : 100;
  686.             if (rate < 0)
  687.             rate = 0;
  688.         } catch (Exception e) {
  689.             rate = 100;
  690.         }
  691.         }
  692.     }
  693.     if (rate >= 0) {
  694.         repaint(rate, 0, 0, width, height);
  695.     }
  696.     return (flags & (ALLBITS|ABORT)) == 0;
  697.     }
  698.  
  699.     /**
  700.      * Creates an image from the specified image producer.
  701.      * @param producer the image producer
  702.      */
  703.     public Image createImage(ImageProducer producer) {
  704.     ComponentPeer peer = this.peer;
  705.     return (peer != null)
  706.         ? peer.createImage(producer)
  707.         : getToolkit().createImage(producer);
  708.     }
  709.  
  710.     /**
  711.      * Creates an off-screen drawable Image to be used for double buffering.
  712.      * @param width the specified width
  713.      * @param height the specified height
  714.      */
  715.     public Image createImage(int width, int height) {
  716.     ComponentPeer peer = this.peer;
  717.     return (peer != null) ? peer.createImage(width, height) : null;
  718.     }
  719.  
  720.     /**
  721.      * Prepares an image for rendering on this Component.  The image
  722.      * data is downloaded asynchronously in another thread and the
  723.      * appropriate screen representation of the image is generated.
  724.      * @param image the Image to prepare a screen representation for
  725.      * @param observer the ImageObserver object to be notified as the
  726.      *        image is being prepared
  727.      * @return true if the image has already been fully prepared
  728.      * @see ImageObserver
  729.      */
  730.     public boolean prepareImage(Image image, ImageObserver observer) {
  731.         return prepareImage(image, -1, -1, observer);
  732.     }
  733.  
  734.     /**
  735.      * Prepares an image for rendering on this Component at the
  736.      * specified width and height.  The image data is downloaded
  737.      * asynchronously in another thread and an appropriately scaled
  738.      * screen representation of the image is generated.
  739.      * @param image the Image to prepare a screen representation for
  740.      * @param width the width of the desired screen representation
  741.      * @param height the height of the desired screen representation
  742.      * @param observer the ImageObserver object to be notified as the
  743.      *        image is being prepared
  744.      * @return true if the image has already been fully prepared
  745.      * @see ImageObserver
  746.      */
  747.     public boolean prepareImage(Image image, int width, int height,
  748.                 ImageObserver observer) {
  749.     ComponentPeer peer = this.peer;
  750.     return (peer != null)
  751.         ? peer.prepareImage(image, width, height, observer)
  752.         : getToolkit().prepareImage(image, width, height, observer);
  753.     }
  754.  
  755.     /**
  756.      * Returns the status of the construction of a screen representation
  757.      * of the specified image.
  758.      * This method does not cause the image to begin loading. Use the
  759.      * prepareImage method to force the loading of an image.
  760.      * @param image the Image to check the status of
  761.      * @param observer the ImageObserver object to be notified as the
  762.      *        image is being prepared
  763.      * @return the boolean OR of the ImageObserver flags for the
  764.      *         data that is currently available
  765.      * @see ImageObserver
  766.      * @see #prepareImage
  767.      */
  768.     public int checkImage(Image image, ImageObserver observer) {
  769.         return checkImage(image, -1, -1, observer);
  770.     }
  771.  
  772.     /**
  773.      * Returns the status of the construction of a scaled screen
  774.      * representation of the specified image.
  775.      * This method does not cause the image to begin loading, use the
  776.      * prepareImage method to force the loading of an image.
  777.      * @param image the Image to check the status of
  778.      * @param width the width of the scaled version to check the status of
  779.      * @param height the height of the scaled version to check the status of
  780.      * @param observer the ImageObserver object to be notified as the
  781.      *        image is being prepared
  782.      * @return the boolean OR of the ImageObserver flags for the
  783.      *         data that is currently available
  784.      * @see ImageObserver
  785.      * @see #prepareImage
  786.      */
  787.     public int checkImage(Image image, int width, int height,
  788.               ImageObserver observer) {
  789.     ComponentPeer peer = this.peer;
  790.     return (peer != null)
  791.         ? peer.checkImage(image, width, height, observer)
  792.         : getToolkit().checkImage(image, width, height, observer);
  793.     }
  794.  
  795.     /**  
  796.      * Checks whether a specified x,y location is "inside" this
  797.      * Component. By default, x and y are inside an Component if
  798.      * they fall within the bounding box of that Component.
  799.      * @param x the x coordinate
  800.      * @param y the y coordinate
  801.      * @see #locate
  802.      */
  803.     public synchronized boolean inside(int x, int y) {
  804.     return (x >= 0) && ((x-this.x) < width) && (y >= 0) && ((y-this.y) < height);
  805.     }
  806.  
  807.     /** 
  808.      * Returns the component or subcomponent that contains the x,y location.
  809.      * @param x the x coordinate
  810.      * @param y the y coordinate
  811.      * @see #inside
  812.      */
  813.     public Component locate(int x, int y) {
  814.     return inside(x, y) ? this : null;
  815.     }
  816.  
  817.     /**
  818.      * Delivers an event to this component or one of its sub components.
  819.      * @param e the event
  820.      * @see #handleEvent
  821.      * @see #postEvent
  822.      */
  823.     public void deliverEvent(Event e) {
  824.     postEvent(e);
  825.     }
  826.  
  827.     /**
  828.      * Posts an event to this component. This will result in a call
  829.      * to handleEvent. If handleEvent returns false the event is
  830.      * passed on to the parent of this component.
  831.      * @param e the event
  832.      * @see #handleEvent
  833.      * @see #deliverEvent
  834.      */
  835.     public boolean postEvent(Event e) {
  836.     ComponentPeer peer = this.peer;
  837.  
  838.     if (handleEvent(e)) {
  839.         return true;
  840.     }
  841.  
  842.     Component parent = this.parent;
  843.     if (parent != null) {
  844.         e.translate(x, y);
  845.         if (parent.postEvent(e)) {
  846.         return true;
  847.         }
  848.     }
  849.     if (peer != null) {
  850.         return peer.handleEvent(e);
  851.     }
  852.  
  853.     return false;
  854.     }
  855.  
  856.     /**
  857.      * Handles the event. Returns true if the event is handled and
  858.      * should not be passed to the parent of this component. The default
  859.      * event handler calls some helper methods to make life easier
  860.      * on the programmer.
  861.      * @param evt the event
  862.      * @see #mouseEnter
  863.      * @see #mouseExit
  864.      * @see #mouseMove
  865.      * @see #mouseDown
  866.      * @see #mouseDrag
  867.      * @see #mouseUp
  868.      * @see #keyDown
  869.      * @see #action
  870.      */
  871.     public boolean handleEvent(Event evt) {
  872.     switch (evt.id) {
  873.       case Event.MOUSE_ENTER:
  874.         return mouseEnter(evt, evt.x, evt.y);
  875.  
  876.       case Event.MOUSE_EXIT:
  877.         return mouseExit(evt, evt.x, evt.y);
  878.  
  879.       case Event.MOUSE_MOVE:
  880.         return mouseMove(evt, evt.x, evt.y);
  881.  
  882.       case Event.MOUSE_DOWN:
  883.         return mouseDown(evt, evt.x, evt.y);
  884.  
  885.       case Event.MOUSE_DRAG:
  886.         return mouseDrag(evt, evt.x, evt.y);
  887.  
  888.       case Event.MOUSE_UP:
  889.         return mouseUp(evt, evt.x, evt.y);
  890.  
  891.       case Event.KEY_PRESS:
  892.       case Event.KEY_ACTION:
  893.         return keyDown(evt, evt.key);
  894.  
  895.       case Event.KEY_RELEASE:
  896.       case Event.KEY_ACTION_RELEASE:
  897.         return keyUp(evt, evt.key);
  898.         
  899.       case Event.ACTION_EVENT:
  900.         return action(evt, evt.arg);
  901.       case Event.GOT_FOCUS:
  902.         return gotFocus(evt, evt.arg);
  903.       case Event.LOST_FOCUS:
  904.         return lostFocus(evt, evt.arg);
  905.     }
  906.     return false;
  907.     }
  908.  
  909.     /**
  910.      * Called if the mouse is down.
  911.      * @param evt the event 
  912.      * @param x the x coordinate
  913.      * @param y the y coordinate
  914.      * @see #handleEvent
  915.      */
  916.     public boolean mouseDown(Event evt, int x, int y) {
  917.     return false;
  918.     }
  919.  
  920.     /**
  921.      * Called if the mouse is dragged (the mouse button is down).
  922.      * @param evt the event
  923.      * @param x the x coordinate
  924.      * @param y the y coordinate
  925.      * @see #handleEvent
  926.      */
  927.     public boolean mouseDrag(Event evt, int x, int y) {
  928.     return false;
  929.     }
  930.  
  931.     /**
  932.      * Called if the mouse is up.
  933.      * @param evt the event
  934.      * @param x the x coordinate
  935.      * @param y the y coordinate
  936.      * @see #handleEvent
  937.      */
  938.     public boolean mouseUp(Event evt, int x, int y) {
  939.     return false;
  940.     }
  941.  
  942.     /**
  943.      * Called if the mouse moves (the mouse button is up).
  944.      * @param evt the event
  945.      * @param x the x coordinate
  946.      * @param y the y coordinate
  947.      * @see #handleEvent
  948.      */
  949.     public boolean mouseMove(Event evt, int x, int y) {
  950.     return false;
  951.     }
  952.  
  953.     /**
  954.      * Called when the mouse enters the component.
  955.      * @param evt the event
  956.      * @param x the x coordinate
  957.      * @param y the y coordinate
  958.      * @see #handleEvent
  959.      */
  960.     public boolean mouseEnter(Event evt, int x, int y) {
  961.     return false;
  962.     }
  963.  
  964.     /**
  965.      * Called when the mouse exits the component.
  966.      * @param evt the event
  967.      * @param x the x coordinate
  968.      * @param y the y coordinate
  969.      * @see #handleEvent
  970.      */
  971.     public boolean mouseExit(Event evt, int x, int y) {
  972.     return false;
  973.     }
  974.  
  975.     /**
  976.      * Called if a character is pressed.
  977.      * @param evt the event
  978.      * @param key the key that's pressed
  979.      * @see #handleEvent
  980.      */
  981.     public boolean keyDown(Event evt, int key) {
  982.     return false;
  983.     }
  984.  
  985.     /**
  986.      * Called if a character is released.
  987.      * @param evt the event
  988.      * @param key the key that's released
  989.      * @see #handleEvent
  990.      */
  991.     public boolean keyUp(Event evt, int key) {
  992.     return false;
  993.     }
  994.  
  995.     /**
  996.      * Called if an action occurs in the Component.
  997.      * @param evt the event
  998.      * @param what the action that's occuring
  999.      * @see #handleEvent
  1000.      */
  1001.     public boolean action(Event evt, Object what) {
  1002.     return false;
  1003.     }
  1004.  
  1005.     /** 
  1006.      * Notifies the Component to create a peer.
  1007.      * @see #getPeer
  1008.      * @see #removeNotify
  1009.      */
  1010.     public void addNotify() {
  1011.     valid = false;
  1012.     }
  1013.  
  1014.     /** 
  1015.      * Notifies the Component to destroy the peer.
  1016.      * @see #getPeer
  1017.      * @see #addNotify
  1018.      */
  1019.     public synchronized void removeNotify() {
  1020.     if (peer != null) {
  1021.         peer.dispose();
  1022.         peer = null;
  1023.     }
  1024.     }
  1025.  
  1026.     /** 
  1027.      * Indicates that this component has received the input focus.
  1028.      * @see #requestFocus
  1029.      * @see #lostFocus
  1030.      */
  1031.     public boolean gotFocus(Event evt, Object what) {
  1032.     return false;
  1033.     }
  1034.  
  1035.     /** 
  1036.      * Indicates that this component has lost the input focus.  
  1037.      * @see #requestFocus
  1038.      * @see #gotFocus
  1039.      */
  1040.     public boolean lostFocus(Event evt, Object what) {
  1041.     return false;
  1042.     }
  1043.  
  1044.     /** 
  1045.      * Requests the input focus. The gotFocus() method will be called
  1046.      * if this method is successful.
  1047.      * @see #gotFocus
  1048.      */
  1049.     public void requestFocus() {
  1050.     ComponentPeer peer = this.peer;
  1051.     if (peer != null) {
  1052.         peer.requestFocus();
  1053.     }
  1054.     }
  1055.  
  1056.     /**
  1057.      * Moves the focus to the next component.
  1058.      * @see #requestFocus
  1059.      * @see #gotFocus
  1060.      */
  1061.      public void nextFocus() {
  1062.     ComponentPeer peer = this.peer;
  1063.      if (peer != null) {
  1064.          peer.nextFocus();
  1065.      }
  1066.      }
  1067.  
  1068.     /**
  1069.      * Returns the parameter String of this Component.
  1070.      */
  1071.     protected String paramString() {
  1072.     String str = x + "," + y + "," + width + "x" + height;
  1073.     if (!valid) {
  1074.         str += ",invalid";
  1075.     }
  1076.     if (!visible) {
  1077.         str += ",hidden";
  1078.     }
  1079.     if (!enabled) {
  1080.         str += ",disabled";
  1081.     }
  1082.     return str;
  1083.     }
  1084.  
  1085.     /**
  1086.      * Returns the String representation of this Component's values.
  1087.      */
  1088.     public String toString() {
  1089.     return getClass().getName() + "[" + paramString() + "]";
  1090.     }
  1091.  
  1092.     /**
  1093.      * Prints a listing to a print stream.
  1094.      */
  1095.     public void list() {
  1096.     list(System.out, 0);
  1097.     }
  1098.  
  1099.     /**
  1100.      * Prints a listing to the specified print out stream.
  1101.      * @param out the Stream name
  1102.      */
  1103.     public void list(PrintStream out) {
  1104.     list(out, 0);
  1105.     }
  1106.  
  1107.     /**
  1108.      * Prints out a list, starting at the specified indention, to the specified 
  1109.      * print stream.
  1110.      * @param out the Stream name
  1111.      * @param indent the start of the list 
  1112.      */
  1113.     public void list(PrintStream out, int indent) {
  1114.     for (int i = 0 ; i < indent ; i++) {
  1115.         out.print("  ");
  1116.     }
  1117.     out.println(this);
  1118.     }
  1119. }
  1120.